/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function (strs) {
    let arr = []
    let obj = {}
    for (let index = 0; index < strs.length; index++) {
        let current = strs[index]
        let str = current.split('').sort().join('');
        if (obj[str] !== undefined) {
            arr[obj[str]].push(current)
        } else {
            let len = arr.length
            obj[str] = len
            arr.push([current])
        }
    }
    return arr
};

console.log(groupAnagrams(['a']));

; (function () {
    //return
    /**
   * @param {string[]} strs
   * @return {string[][]}
   */
    var groupAnagrams = function (strs) {
        const letter2num = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101];

        // 利用质素相乘的总积,来作为哈希值
        const getValue = function (str) {
            let res = 1;
            for (let i = 0; i < str.length; i++) {
                //charCodeAt() 返回的是字符的 Unicode 编码
                //'a'.charCodeAt(0 这个返回的就是97
                res *= letter2num[str.charCodeAt(i) - 'a'.charCodeAt(0)];
            }
            return res;
        }


        let map = new Map();
        for (let str of strs) {
            let value = getValue(str);
            if (!map.has(value)) {
                map.set(value, []);
            }
            let arr = map.get(value);
            arr.push(str);
            map.set(value, arr);
        }

        let resultArr = Array.from(map.values());
        return resultArr;

    };
})();